package cn.iocoder.yudao.module.famous.doctor.controller.app.performance;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.famous.doctor.controller.app.operator.utils.Query;
import cn.iocoder.yudao.module.famous.doctor.controller.app.performance.vo.PerformanceResult;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.form.UserProjectResultEntity;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.form.UserProjectResultMapper;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.performance.PerformanceResultMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import dm.jdbc.util.StringUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 后台-绩效管理-填报记录
 *
 * @author wyx
 * @since 2021-08-18
 * */


@Validated
@RestController
@RequestMapping("/manager/performance/result")
@Tag(name = "后台-绩效管理-填报记录")
public class PerformanceResultController {


/*	@Log("绩效管理-填报记录 - 保存")
	@PostMapping("/save")
	@ApiOperation("绩效管理-填报记录 - 保存")
	public Result<PerformanceResult> save(@RequestBody PerformanceResult performanceResult) {
		if (performanceResult.getId() == null) {
			performanceResultMapper.insert(performanceResult);
		}
		else
			performanceResultMapper.updateById(performanceResult);
		return Result.ok(performanceResult);
	}*/


	@GetMapping("/list")
	@Parameter(name = "pageNum", description = "当前页码", required = false, example = "1")
	@Parameter(name = "pageSize", description = "每页显示记录数", required = false, example = "1")
	@Parameter(name = "operatorName", description = "运营商名称", required = false, example = "1")
	@Parameter(name = "operatorPhone", description = "运营商手机号", required = false, example = "1")
	@Parameter(name = "name", description = "业务名称", required = false, example = "1")
	@Parameter(name = "title", description = "发布标题", required = false, example = "1")
	public CommonResult<IPage<PerformanceResult>> list(@RequestParam(required = false) Map<String, Object> params) {
		String operatorName = params.containsKey("operatorName") ? params.get("operatorName").toString().trim() : null;
		String operatorPhone = params.containsKey("operatorPhone") ? params.get("operatorPhone").toString().trim() : null;
		String title = params.containsKey("title") ? params.get("title").toString().trim() : null;
		String name = params.containsKey("name") ? params.get("name").toString().trim() : null;

		IPage<PerformanceResult> page = performanceResultMapper.selectPage(new Query().getPage(params),new QueryWrapper<PerformanceResult>()
				.like(StringUtil.isNotEmpty(operatorName),"operator_name",operatorName)
				.like(StringUtil.isNotEmpty(operatorPhone), "operator_phone", operatorPhone)
				.like(StringUtil.isNotEmpty(title),"title",title)
				.like(StringUtil.isNotEmpty(name), "name", name));
		return success(page);
	}

	@DeleteMapping("/delete")
	@Operation(summary="绩效管理-填报记录 - 删除")
	public CommonResult delete(Long id) {
		performanceResultMapper.deleteById(id);
		return success(id);
	}

	@GetMapping("/view")
	@Operation(summary="绩效管理-填报记录 - 详情")
	public CommonResult<UserProjectResultEntity> view(Long CommonResultId) {
		UserProjectResultEntity userProjectCommonResultEntity = userProjectResultMapper.selectById(CommonResultId);
		return success(userProjectCommonResultEntity);
	}


	@Operation(summary ="绩效填报-提交")
	@PostMapping("/performanceSave")
	public CommonResult<Integer> performanceSave(@RequestBody UserProjectResultEntity userProjectResultEntity) {
		Long loginUserId = getLoginUserId();
		return success(userProjectResultMapper.insert(userProjectResultEntity));
	}

	@Autowired
	private PerformanceResultMapper performanceResultMapper;
	@Autowired
	private UserProjectResultMapper userProjectResultMapper;
}
